import { compareDay } from "../../../@vant/weapp/dist/calendar/utils"
import axios from "../../utils/axios";

// pages/comment/comment.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments:[],
    dynamicdetail:{},
    activename:'',
    firstcomments:[],
    secondcomments:[]
  },
  getsecondcommentitem(parentcomments:AnyArray,parentid:Number,comments:AnyArray){
    comments.forEach((item,index) => {
      if(item.parentId==parentid){
        parentcomments.push(item)
        comments.splice(index,1)
        return this.getsecondcommentitem(parentcomments,item.id,comments)
      }
    });
    return parentcomments
  },
  changecomment(e){
    this.setData({
      activename:e.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
 async  onLoad(option) {
    var dynamicdetail=JSON.parse(option.dynamicdetail)
    var token=getApp().globalData.accessToken
  var result=  await axios.get("/dynamics/comment?id="+dynamicdetail.id)
      this.setData({
        dynamicdetail:dynamicdetail,
        comments:result
      })
      console.log(this.data.comments)
      var firstcomments=[]
      var comments=this.data.comments
      comments.forEach((item,index)=>{
        if(!item.parentId){
          firstcomments.push(item)
          comments.splice(index,1)
        }
      })
      console.log(firstcomments)
      console.log(comments)
      var secondcomments=[]
      firstcomments.forEach(item=>{
        var secondcommentitems=[]
        comments.forEach(it=>{    
          if(item.id==it.parentId){
            var secondcommentitem=[]
            secondcommentitem.push(it)
           secondcommentitem=this.getsecondcommentitem(secondcommentitem,it.id,comments)
            secondcommentitems.push(secondcommentitem)
          }
        })
        secondcomments.push(secondcommentitems)
      })
      this.setData({
        firstcomments:firstcomments,
        secondcomments:secondcomments
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})